<template>
  <view>
    <BaseNavbar :title="title" />
    <view class="content">
      <view>
        <view class="title" v-if="false">当前选择商品</view>
        <BaseInput v-if="false" placeholder="请选择商品" rightText="arrow" v-model="goodsName" :disabled="true"
          @onClick="selectGoods" />
        <image class="goods-img" :src="goodsImg" />
      </view>
      <view>
        <view class="title">套餐名称</view>
        <BaseInput placeholder="请输入商品名" v-model="aliName" />
      </view>
      <view>
        <view class="title">价格</view>
        <BaseInput placeholder="请输入价格" rightText="元" v-model="goodsPrice" />
      </view>
      <view v-if="false">
        <view class="title">最大库存</view>
        <BaseInput placeholder="请输入最大库存" v-model="goodsDefaultStock" />
      </view>
      <view v-if="false">
        <view class="title">单位</view>
        <BaseRadio :list="unitListRadio" :radioIndex.sync="selectUnitRadio" />
      </view>
      <block v-if="false">
        <view>
          <view class="title">免费长度(厘米/cm)</view>
          <BaseInput placeholder="请输入免费长度" v-model="towelNorms.free_lenth" />
        </view>
        <view>
          <view class="title">购买长度(厘米/cm)</view>
          <BaseInput placeholder="请输入购买长度" v-model="towelNorms.pay_lenth" />
        </view>
      </block>
      <!-- <block v-if="selectUnitRadio === 3">
        <view>
          <view class="title">游戏时长</view>
          <BaseInput
            placeholder="请输入游戏时间"
            v-model="game_time"
          />
        </view>
      </block> -->
      <block>
        <view>
          <view class="title">骑行时长</view>
          <BaseInput placeholder="请输入游戏时间" v-model="game_time" rightText="分钟" />
        </view>
      </block>
      <view class="btn">
        <BaseButton type="primary" @onClick="confirm">确 认 修 改</BaseButton>
      </view>
    </view>
  </view>
</template>

<script>
import BaseButton from "../../components/base/BaseButton.vue";
import BaseInput from "../../components/base/BaseInput.vue";
import BaseNavbar from "../../components/base/BaseNavbar.vue";
import BaseRadio from "../../components/base/BaseRadio.vue";
export default {
  components: { BaseNavbar, BaseButton, BaseInput, BaseRadio },
  data() {
    return {
      title: "编辑套餐",
      goodsInfo: {  },
      trackNum: Number, //套餐号
      goodsName: "",
      aliName: "",
      goodsPrice: "",
      goodsDefaultStock: "",
      goodsId: 1,
      device_sn: "", //设备编号
      isFromTemplatePlace: false, //来自商品模板
      original_img:'',
      unitListRadio: [
        {
          title: "数量",
          name: 1,
          selectIndex: 0,
        },
        {
          title: "长度(CM)",
          name: 2,
          selectIndex: 1,
        },
        {
          title: "时间(分钟)",
          name: 3,
          selectIndex: 2,
        },
      ],
      selectUnitRadio: 3,
      towelNorms: {
        free_lenth: 0, //免费长度
        pay_lenth: 0, //购买长度
      },
      game_time: 1, // 游戏时长
    };
  },
  computed: {
    goodsImg() {
      return (
        this.original_img || require("@/static/img/errorLoad.png")
      );
    },
  },
  methods: {
    confirm() {
      if (this.game_time <= 0) {
        return this.isShowErr('骑行时长要大于0')
      }
      if (this.isFromTemplatePlace) {
        let pages = getCurrentPages();
        let currPage = pages[pages.length - 1]; //当前页面
        let prevPage = pages[pages.length - 2]; //上一个页面
        console.log("选择的单位类型", this.selectUnitRadio);
        console.log("选择的单位类型", this.selectUnitRadio - 1);
        //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
        prevPage.setData({
          item: {
            num: Number(this.trackNum),
            goods_id: this.goodsId,
            good_price: this.goodsPrice || 0.0,
            default_stock: Number(this.goodsDefaultStock) || 1,
            goods_name: this.aliName,

            original_img: this.original_img,
            // unitName: this.unitListRadio[this.selectUnitRadio - 1].title,
            unit: this.selectUnitRadio,
            free_lenth: this.towelNorms.free_lenth,
            pay_lenth: this.towelNorms.pay_lenth,
            game_time: this.game_time,
          },
        });
        uni.navigateBack({ delta: 1 });
      } else {
        let item = {
          num: Number(this.trackNum),
          good_id: this.goodsId,
          good_price: this.goodsPrice || 0.0,
          default_stock: Number(this.goodsDefaultStock) || 1,
          name: this.aliName,
          unit: this.selectUnitRadio,
          free_lenth: this.towelNorms.free_lenth,
          pay_lenth: this.towelNorms.pay_lenth,
          game_time: this.game_time,
        };
        if (item.unit === 2 || item.unit === 3) {
          item = {
            ...item,
            ...this.towelNorms,
          };
        }
        let list = [item];
        let data = {
          device_sn: this.device_sn || "",
          items: list,
          delNotFound: false,
        };
        this.$u.api.saveUMShops(data).then((res) => {
          this.isShowSuccess("修改成功", 1, () => { }, true);
        });
      }
    },
    selectGoods() {
      uni.navigateTo({ url: `/pagesB/goods/GoodsList?from=deviceSelectGoods` });
    },
    setGoodsInfo() {
      this.goodsName = this.goodsInfo.goods_name;
      this.aliName = this.goodsInfo.goods_name;
      this.goodsPrice = this.goodsInfo.good_price;
      // this.goodsDefaultStock = this.goodsInfo.default_stock;
      // this.goodsId = this.goodsInfo.goods_id;
    },
  },
  onLoad(opt) {
    console.log("选择单位 1 ", this.selectUnitRadio, this.isFromTemplatePlace);
    if (opt?.from == "template_place") {
      this.isFromTemplatePlace = true;
    }
    if (opt?.goodsInfo) {

      this.goodsInfo = JSON.parse(opt.goodsInfo);
      console.log("获取的参数", this.goodsInfo);
      this.trackNum = opt.num;
      this.device_sn = opt?.device_sn === "undefined" ? "" : opt.device_sn;
      this.setGoodsInfo();

      //设置单位单选框数据
      // this.selectUnitRadio = this.goodsInfo?.unit;
      this.towelNorms = {
        free_lenth: this.goodsInfo?.free_lenth, //免费长度
        pay_lenth: this.goodsInfo?.pay_lenth, //购买长度
      };

      this.game_time = this.goodsInfo?.game_time; // 游戏时长
    }
    // if(this.selectUnitRadio === "undefined" || this.selectUnitRadio == undefined ){
    //   this.selectUnitRadio = 1;
    // }
    let data = {

      good_id: 1
    }
    try {

      this.$u.api.getMyParentProducts(data).then(res => {
        console.log('返回信息', res)
        this.original_img=res.data[0].original_img
        // this.$forceUpdate()
      })
    }
    catch (err) {
      console.log('返回错误信息', err)
    }
    console.log("选择单位 2 ", this.selectUnitRadio, this.isFromTemplatePlace);
  },
  onShow(e) {
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1]; // 当前页
    if (currPage.data.isGetGoodsInfo) {
      currPage.data.isGetGoodsInfo = false;
      this.goodsInfo = currPage.data.goodsInfo;
      this.setGoodsInfo();
    }
  },
};
</script>

<style lang="scss" scoped>
.content {
  padding: 30rpx;

  .title {
    color: $textBlack;
    font-size: $font-size-middle;
    font-weight: bold;
    margin: 50rpx 0 20rpx;
  }

  .input {
    height: 80rpx;
    background-color: #f2f2f2;
  }

  .goods-img {
    width: 150rpx;
    height: 150rpx;
    border-radius: $imgRadius;
    // background-color: pink;
    margin-top: 30rpx;
  }

  .btn {
    margin-top: 100rpx;
  }
}
</style>